//
//  CSDNUser.m
//  CsdnPlus
//
//  Created by 孟瑞瑞 on 2018/5/26.
//  Copyright © 2018年 CSDN. All rights reserved.
//

#import "CSDNUser.h"
#import "CNViewConst.h"

@interface CSDNUser()<NSCoding>

@end

@implementation CSDNUser

+ (instancetype)currentUser
{
    static CSDNUser *singletonUser = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSData *encodedObject = [[NSUserDefaults standardUserDefaults] objectForKey:@"CNCurrentUser"];
        if (encodedObject) {
            singletonUser = [NSKeyedUnarchiver unarchiveObjectWithData:encodedObject];
        } else {
            singletonUser = [[self alloc] init];
        }
    });
    return singletonUser;
}

- (NSString *)sessionId{
    if (!_sessionId) {
        return @"";
    }else
        return _sessionId;
    
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
    
    [aCoder encodeObject:self.username ?: [NSNull null] forKey:@"username"];
    [aCoder encodeObject:self.nickname ?: [NSNull null] forKey:@"nickname"];
    [aCoder encodeObject:self.sessionId ?: [NSNull null] forKey:@"sessionId"];
    [aCoder encodeObject:self.avatarUrl ?: [NSNull null] forKey:@"avatarUrl"];
    [aCoder encodeObject:self.expireTime ?: [NSNull null] forKey:@"expireTime"];
    [aCoder encodeObject:self.userId ?: [NSNull null] forKey:@"userId"];
    [aCoder encodeObject:self.userInfo ?: [NSNull null] forKey:@"userInfo"];
    [aCoder encodeObject:self.tgc ?: [NSNull null] forKey:@"tgc"];
    [aCoder encodeObject:self.mobile ?: [NSNull null] forKey:@"mobile"];
    [aCoder encodeObject:self.email ?: [NSNull null] forKey:@"email"];
    [aCoder encodeObject:self.selfdesc ?: [NSNull null] forKey:@"selfdesc"];
    
    [aCoder encodeObject:self.school ?: [NSNull null] forKey:@"school"];
    [aCoder encodeObject:self.company ?: [NSNull null] forKey:@"company"];
    [aCoder encodeObject:self.realName ?: [NSNull null] forKey:@"realName"];

    [aCoder encodeObject:self.gender ?: @(0) forKey:@"gender"];
    [aCoder encodeObject:self.IMToken ?: [NSNull null] forKey:@"IMToken"];

}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    if (self) {
        self.username = [aDecoder decodeObjectForKey:@"username"];
        if ([self.username isEqual:[NSNull null]]) {
            self.username = nil;
        }
        self.nickname = [aDecoder decodeObjectForKey:@"nickname"];
        if ([self.nickname isEqual:[NSNull null]]) {
            self.nickname = nil;
        }
        self.sessionId = [aDecoder decodeObjectForKey:@"sessionId"];
        if ([self.sessionId isEqual:[NSNull null]]) {
            self.sessionId = nil;
        }
        
        self.avatarUrl = [aDecoder decodeObjectForKey:@"avatarUrl"];
        if ([self.avatarUrl isEqual:[NSNull null]]) {
            self.avatarUrl = nil;
        }
        self.expireTime = [aDecoder decodeObjectForKey:@"expireTime"];
        if ([self.expireTime isEqual:[NSNull null]]) {
            self.expireTime = nil;
        }
        self.userId = [aDecoder decodeObjectForKey:@"userId"];
        if ([self.userId isEqual:[NSNull null]]) {
            self.userId = nil;
        }
        self.userInfo = [aDecoder decodeObjectForKey:@"userInfo"];
        if ([self.userInfo isEqual:[NSNull null]]) {
            self.userInfo = nil;
        }
        
        self.tgc = [aDecoder decodeObjectForKey:@"tgc"];
        if ([self.tgc isEqual:[NSNull null]]) {
            self.tgc = nil;
        }
        
        self.mobile = [aDecoder decodeObjectForKey:@"mobile"];
        if ([self.mobile isEqual:[NSNull null]]) {
            self.mobile = nil;
        }
        
        self.email = [aDecoder decodeObjectForKey:@"email"];
        if ([self.email isEqual:[NSNull null]]) {
            self.email = nil;
        }
        
        self.selfdesc = [aDecoder decodeObjectForKey:@"selfdesc"];
        if ([self.selfdesc isEqual:[NSNull null]]) {
            self.selfdesc = nil;
        }
        self.school = [aDecoder decodeObjectForKey:@"school"];
        if ([self.school isEqual:[NSNull null]]) {
            self.school = nil;
        }
        self.realName = [aDecoder decodeObjectForKey:@"realName"];
        if ([self.realName isEqual:[NSNull null]]) {
            self.realName = nil;
        }
        self.company = [aDecoder decodeObjectForKey:@"company"];
        if ([self.company isEqual:[NSNull null]]) {
            self.company = nil;
        }
        self.gender = [aDecoder decodeObjectForKey:@"gender"];
        
        self.IMToken = [aDecoder decodeObjectForKey:@"IMToken"];
        if ([self.IMToken isEqual:[NSNull null]]) {
            self.IMToken = nil;
        }
        
   
    }
    return self;
}

- (void)save
{
    NSData *archivedData = [NSKeyedArchiver archivedDataWithRootObject:self];
    [[NSUserDefaults standardUserDefaults] setObject:archivedData forKey:@"CNCurrentUser"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)reset
{
    self.username = nil;
    
    self.nickname = nil;
    
    self.sessionId = nil;
    
    self.avatarUrl = nil;
    
    self.expireTime = nil;
    
    self.userId = nil;
    
    self.userInfo = nil;
    
    self.tgc = nil;
    
    self.mobile = nil;
    
    self.email = nil;
    
    self.selfdesc = nil;
    self.company = nil;
    self.school = nil;
    self.gender = 0;
    self.IMToken = nil;
    self.realName = nil;
    [self save];
    [CNViewConst loginOut];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"CNUserLogoutNotification" object:nil];
    
    
}

@end
